package com.tommy.ddp.finder
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.MainManager;
	import com.tommy.ddp.box.BoxFactory;
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.AxisConfig;
	import com.tommy.ddp.definition.BoxColorType;
	import com.tommy.ddp.definition.BoxExeType;
	import com.tommy.ddp.definition.DDPConfig;
	import com.tommy.ddp.events.FinderEvent;
	import com.tommy.ddp.manager.SwapManager;
	
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	
	import org.taomee.ds.HashMap;
	
	

	public class AdvFinder extends EventDispatcher
	{
		private var x_array:Vector.<FinderVo>;
		private var y_array:Vector.<FinderVo>;
		//剔除的数组
		private var exceptVect:Vector.<FinderVo>;
		private var _disposeVect:Vector.<DisposeVo>;
		private var _crossedHashMap:HashMap;
		
		public function AdvFinder()
		{
			
		}
		public function execute():void
		{
			x_array = new Vector.<FinderVo>();
			y_array = new Vector.<FinderVo>();
			
			exceptVect = new Vector.<FinderVo>();
			_disposeVect = new Vector.<DisposeVo>();
			_crossedHashMap = new HashMap();
			
			findX();
			findY();
			
			findOutCross();
			finalMix();
		}
		
		public function dispose():void
		{
			_crossedHashMap.clear();
			_crossedHashMap = null;
			x_array = null;
			y_array = null;
			exceptVect = null;
			//createNewVect = null;
			_disposeVect = null;
		}
		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get disposeVect():Vector.<DisposeVo>
		{
			return _disposeVect;
		}
		
		private function findX():void
		{
			for(var i:uint = 0;i<DDPConfig.H_NUM;i++)
			{
				var begin:uint = 0;
				var end:uint = begin+1;
				while(end < DDPConfig.W_NUM)
				{
					var startBox:DdpBox = GridDataManager.getBox(begin,i);
					var nextBox:DdpBox = GridDataManager.getBox(end,i);
					while(nextBox && startBox && startBox.colorType == nextBox.colorType && startBox.exeType!= BoxExeType.BOOM_CLONE  && nextBox.exeType != BoxExeType.BOOM_CLONE)
					{
						end += 1;
						nextBox = GridDataManager.getBox(end,i);
					}
					if(end - begin >= 3)
					{
						var vo:FinderVo = new FinderVo();
						vo.axis = AxisConfig.X_AXIS;
						vo.y = i;
						vo.start = begin;
						vo.end = end -1;
						x_array.push(vo);
					}
					begin = end;
					end = begin+1;
				}
			}
		}
		private function findY():void
		{
			for(var i:uint = 0;i<DDPConfig.W_NUM;i++)
			{
				var begin:uint = 0;
				var end:uint = begin+1;
				while(end < DDPConfig.H_NUM)
				{
					var startBox:DdpBox = GridDataManager.getBox(i,begin);
					var nextBox:DdpBox = GridDataManager.getBox(i,end);
					while(nextBox && startBox && startBox.colorType == nextBox.colorType)
					{
						end += 1;
						nextBox = GridDataManager.getBox(i,end);
					}
					if(end - begin >= 3)
					{
						var vo:FinderVo = new FinderVo();
						vo.axis = AxisConfig.Y_AXIS;
						vo.x = i;
						vo.start = begin;
						vo.end = end -1;
						y_array.push(vo);
					}
					begin = end;
					end = begin+1;
				}
			}
		}
		
		
		private function findOutCross():void
		{
			
			for each(var i:FinderVo in x_array)
			{
				if(exceptVect.indexOf(i) == -1)
				{
					for each(var j:FinderVo in y_array)
					{
						//先判断有没有5个以上的，有的话把与5个以上交叉的剔除
						//有交叉的把交叉点记录下来，生成一个新的
						//把所有的BOX找出来，去重，消除
						if(exceptVect.indexOf(j) == -1)
						{
							var p:Point = FinderVo.isCross(i,j);
							if(p)
							{
								if(i.length >= 5)
								{
									exceptVect.push(j);
								}
								else if(j.length >= 5)
								{
									exceptVect.push(i);
								}
								else
								{
									var x:uint = x_array.indexOf(i);
									var y:uint = y_array.indexOf(j);
									
									if(!_crossedHashMap.containsKey(x+"_"+y))
									{
										//创建一个交叉的炸弹，记录创建的点
										i.isCrossed = j.isCrossed = true;
										var box:DdpBox = GridDataManager.getBox(p.x,p.y);
										//createNewVect.push(new CreateNewVo(BoxExeType.BOOM,box.colorType,p));
										//
										var createNewVo:CreateNewVo = new CreateNewVo(BoxExeType.BOOM,box.colorType,p);
										var vo:DisposeVo = new DisposeVo();
										vo.type = BoxExeType.BOOM;
										vo.addFinderVo(i,j);
										vo.createNewVo = createNewVo;
										_disposeVect.push(vo);
										//
										_crossedHashMap.add(x+"_"+y,true);
										
										findOutCross();
										break;
									}
								}
							}
						}
					}
				}
			}
		}
		
		private function finalMix():void
		{
			mixX();
			mixY();
			
			if(x_array.length == 0 && y_array.length == 0)
			{
				dispatchEvent(new FinderEvent(FinderEvent.NONE_TO_DISPOSE));
			}
			else
			{
				dispatchEvent(new FinderEvent(FinderEvent.GET_DISPOSE));
			}
			//createNew();
		}
		/**
		 * X轴方向可消除的处理
		 * 
		 */		
		private function mixX():void
		{
			for each(var i:FinderVo in x_array)
			{
				if(exceptVect.indexOf(i) == -1)
				{
					var createNewVo:CreateNewVo;
					
					if(!i.isCrossed && i.length == 4)
					{
						//是否由交换来的，记录交换的棋子产生的位置，在此位置生成新的
						var box:DdpBox;
						if(FinderVo.containBox(SwapManager.newBox,i,AxisConfig.X_AXIS))
							box = SwapManager.newBox;
						else if(FinderVo.containBox(SwapManager.orgBox,i,AxisConfig.X_AXIS))
							box = SwapManager.orgBox;
						//
						var arr:Array = [BoxExeType.X_AXIS_ALL,BoxExeType.Y_AXIS_ALL];
						var r:uint = Math.floor(Math.random()*2);
						
						if(box)
						{	
							//createNewVect.push(new CreateNewVo(arr[r],box.colorType,new Point(box.column,box.row)));
							createNewVo = new CreateNewVo(arr[r],box.colorType,new Point(box.column,box.row));
							addDisposeVo(i,arr[r],createNewVo);
						}
						else
						{
							box = GridDataManager.getBox(i.start,i.y);
							//createNewVect.push(new CreateNewVo(arr[r],box.colorType,new Point(i.start,i.y)));
							createNewVo = new CreateNewVo(arr[r],box.colorType,new Point(i.start,i.y));
							addDisposeVo(i,arr[r],createNewVo);
						}
					}
					else if(i.length >= 5)
					{
						//createNewVect.push(new CreateNewVo(BoxExeType.SUPER_BOOM,BoxColorType.NONE,new Point(i.end,i.y)));
						createNewVo = new CreateNewVo(BoxExeType.SUPER_BOOM,BoxColorType.NONE,new Point(i.end,i.y));
						addDisposeVo(i,BoxExeType.SUPER_BOOM,createNewVo);
					}
					else
					{
						if(!i.isCrossed)
							addDisposeVo(i,BoxExeType.NONE);
					}
				}
				//|||改为最后序列处理
				//i.disposeBox(AxisConfig.X_AXIS);
			}
		}
		/**
		 * 往需要最终消除的序列中添加一个数据
		 * @param finderVo
		 * @param createNewVo
		 * 
		 */		
		private function addDisposeVo(finderVo:FinderVo,type:uint,createNewVo:CreateNewVo = null):void
		{
			var vo:DisposeVo = new DisposeVo();
			vo.addFinderVo(finderVo);
			vo.type = type;
			vo.createNewVo = createNewVo;
			_disposeVect.push(vo);
		}
		
		/**
		 * Y轴方向可消除的处理
		 * 
		 */		
		private function mixY():void
		{
			for each(var i:FinderVo in y_array)
			{
				if(exceptVect.indexOf(i) == -1)
				{
					var createNewVo:CreateNewVo;
					
					if(!i.isCrossed && i.length == 4)
					{
						//是否由交换来的，记录交换的棋子产生的位置，在此位置生成新的
						var box:DdpBox;
						if(FinderVo.containBox(SwapManager.newBox,i,AxisConfig.Y_AXIS))
							box = SwapManager.newBox;
						else if(FinderVo.containBox(SwapManager.orgBox,i,AxisConfig.Y_AXIS))
							box = SwapManager.orgBox;
						//
						var arr:Array = [BoxExeType.X_AXIS_ALL,BoxExeType.Y_AXIS_ALL];
						var r:uint = Math.floor(Math.random()*2);
						
						if(box)
						{
							//createNewVect.push(new CreateNewVo(arr[r],box.colorType,new Point(box.column,box.row)));
							createNewVo = new CreateNewVo(arr[r],box.colorType,new Point(box.column,box.row));
							addDisposeVo(i,arr[r],createNewVo);
						}
						else
						{
							box = GridDataManager.getBox(i.x,i.end);
							//createNewVect.push(new CreateNewVo(arr[r],box.colorType,new Point(i.x,i.end)));
							createNewVo = new CreateNewVo(arr[r],box.colorType,new Point(i.x,i.end));
							addDisposeVo(i,arr[r],createNewVo);
						}
					}
					else if(i.length >= 5)
					{
						//createNewVect.push(new CreateNewVo(BoxExeType.SUPER_BOOM,BoxColorType.NONE,new Point(i.x,i.end)));
						createNewVo = new CreateNewVo(BoxExeType.SUPER_BOOM,BoxColorType.NONE,new Point(i.x,i.end));
						addDisposeVo(i,BoxExeType.SUPER_BOOM,createNewVo);
					}
					else
					{
						if(!i.isCrossed)
							addDisposeVo(i,BoxExeType.NONE);
					}
				}
				//|||改为最后序列处理
				//i.disposeBox(AxisConfig.Y_AXIS);
			}
		}
		
		
		/*|||private function createNew():void
		{
			for each(var i:CreateNewVo in createNewVect)
			{
				var ddpBox:DdpBox = BoxFactory.createRandomBox(i.point.x,i.point.y);
				ddpBox.setType(i.colorType,i.exeType);
				
				MainManager.game.container.addChild(ddpBox);
			}
		}*/
	}
}